package com.zx.system.block.controller.system;

import com.zx.core.wrapper.ListWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.system.block.param.DeptParam;
import com.zx.system.block.service.ISysDeptService;
import com.zx.system.block.vo.SysDeptVO;
import com.zx.system.block.vo.TreeVO;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Create;
import com.zx.validation.groups.StatusUpdate;
import com.zx.validation.groups.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/dept")
@RequiredArgsConstructor
public class SysDeptController {
    private final ISysDeptService iSysDeptService;


    /**
     * 获取部门树
     *
     * @param deptParam 部门信息
     * @return {@link ListWrapper< TreeVO >}
     * @author zhou  xun
     * @since 2024-10-03
     */
    @PreAuthorize("hasAuthority('system:dept:list')")
    @OperateLog(value = "获取部门树")
    @GetMapping("getDeptTree")
    public ListWrapper<TreeVO> getDeptTree(DeptParam deptParam) {
        return iSysDeptService.getDeptTree(deptParam);
    }

    /**
     * 查询部门列表
     *
     * @param deptParam 部门信息
     * @return {@link ListWrapper<SysDeptVO>}
     * @author zhou  xun
     * @since 2024-10-03
     */
    @PreAuthorize("hasAuthority('system:dept:list')")
    @OperateLog(value = "查询部门列表")
    @GetMapping("/list")
    public ListWrapper<SysDeptVO> list(DeptParam deptParam) {
        return iSysDeptService.selectDeptVOList(deptParam);
    }

    /**
     * 新增部门
     *
     * @param deptParam 部门信息
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-03
     */
    @PreAuthorize("hasAuthority('system:dept:add')")
    @OperateLog(value = "新增部门")
    @PostMapping("/addDept")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "部门参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Create.class})
    )
    public ResponseWrapper addDept(@RequestBody DeptParam deptParam) {
        return iSysDeptService.addDept(deptParam);
    }

    /**
     * 编辑部门
     *
     * @param deptParam 部门信息
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-03
     */
    @PreAuthorize("hasAuthority('system:dept:edit')")
    @OperateLog(value = "编辑部门")
    @PutMapping("/editDept")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "部门参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Update.class})
    )
    public ResponseWrapper editDept(@RequestBody DeptParam deptParam) {
        return iSysDeptService.editDept(deptParam);
    }

//    /**
//     * 修改部门状态
//     *
//     * @param jsonStr 参数json字符串
//     * @return {@link ResponseWrapper}
//     * @author zhou  xun
//     * @since 2024-10-07
//     */
//    @PreAuthorize("hasAuthority('system:dept:edit')")
//    @OperateLog(value = "修改部门状态")
//    @PutMapping("/editDeptStatus")
//    public ResponseWrapper editDeptStatus(@RequestBody String jsonStr) {
//        ObjectWrapper<DeptParam> objectWrapper = JsonStrUtils.checkParseObject(jsonStr, DeptParam.class);
//        if (!objectWrapper.verifySuccess()) {
//            return objectWrapper;
//        }
//        DeptParam deptParam = objectWrapper.getData();
//        return iSysDeptService.editDeptStatus(deptParam.getId(), deptParam.getStatus());
//    }

    /**
     * 修改部门状态
     *
     * @param deptParam 部门参数
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-07
     */
    @PreAuthorize("hasAuthority('system:dept:edit')")
    @OperateLog(value = "修改部门状态")
    @PutMapping("/editDeptStatus")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "部门参数", rules = {ValidateRulesEnum.OBJECT}, groups = {StatusUpdate.class})
    )
    public ResponseWrapper editDeptStatus(@RequestBody DeptParam deptParam) {
        return iSysDeptService.editDeptStatus(deptParam.getId(), deptParam.getStatus());
    }

    /**
     * 删除部门
     *
     * @param deptId 部门id
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-03
     */
    @PreAuthorize("hasAuthority('system:dept:remove')")
    @OperateLog(value = "删除部门")
    @DeleteMapping("/deleteDept/{deptId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "部门id", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ResponseWrapper deleteDept(@PathVariable("deptId") String deptId) {
        return iSysDeptService.deleteDept(deptId);
    }
}